package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ghost.GhostModule;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_GHOST_ATTACK_BOSS, desc = "攻击boss")
public class GhostAttackBossCmd extends AbstractCommand {

    private int patronsId;
    private int bossId;
    private int attackTimes;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.AttackBossReqMsg reqMsg = GhostActivityProto.AttackBossReqMsg.parseFrom(packet.getBytes());
        patronsId = reqMsg.getPatronsId();
        bossId = reqMsg.getBossId();
        attackTimes = reqMsg.getCostAttackTimes();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!GhostMgr.inOpenTime()) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        GhostMgr.checkUserData(player);

        ret = player.getModule(GhostModule.class).attackBoss(patronsId, bossId, attackTimes);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            GhostActivityProto.AttackBossRespMsg.Builder builder = GhostActivityProto.AttackBossRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_GHOST_ATTACK_BOSS, builder);
        }
    }
}
